package com.huoniu.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huoniu.api.entity.User;
import com.huoniu.api.entity.dto.UserKeyWordDTO;
import com.huoniu.api.entity.vo.UserVO;
import com.huoniu.api.enums.Department;
import com.huoniu.api.enums.Job;
import com.huoniu.api.enums.Level;
import com.huoniu.api.mapper.UserMapper;
import com.huoniu.api.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @类 名： UserServiceImpl <br/>
 * @描 述： <br/>
 * @日 期： 2020/9/2 14:28<br/>
 * @作 者： 侯建军<br/>
 * @版 本： 1.0.0
 * @since JDK 1.8
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean insert(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        List<User> users = userMapper.selectList(queryWrapper);
        if (users.size() > 0) {
            return false;
        }
        userMapper.insert(user);
        return true;
    }

    @Override
    public boolean update(User user) {
        if (userMapper.updateById(user) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        if (userMapper.deleteById(id) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<User> getUsers(UserKeyWordDTO userKeyWordDTO) {
        // 分页
        Page page = new Page<>(userKeyWordDTO.getCurrentPage(), userKeyWordDTO.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        // 条件
        if (!StringUtils.isEmpty(userKeyWordDTO.getUsername())) {
            queryWrapper.like("username", userKeyWordDTO.getUsername());
        }

        if (!StringUtils.isEmpty(userKeyWordDTO.getDepartment())) {
            queryWrapper.eq("department", userKeyWordDTO.getDepartment());
        }

        if (!StringUtils.isEmpty(userKeyWordDTO.getLevel())) {
            queryWrapper.eq("level", userKeyWordDTO.getLevel());
        }
        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
        return userIPage;
    }

    @Override
    public IPage<UserVO> getUserList(UserKeyWordDTO userKeyWordDTO) {
        Page<UserVO> page = new Page<>();
        userKeyWordDTO.setCurrentPage((userKeyWordDTO.getCurrentPage() - 1) * userKeyWordDTO.getPageSize());
        List<UserVO> list = userMapper.getUserList(userKeyWordDTO);
        List<UserVO> userVOList = new ArrayList<>();
        for (UserVO userVO : list) {
            // 进行一个转换，将我们的code 转为name
            userVO.setDepartment(Department.getNameByCode(userVO.getDepartment()));
            userVO.setJob(Job.getNameByCode(userVO.getJob()));
            userVO.setLevel(Level.getNameByCode(userVO.getLevel()));
            userVOList.add(userVO);
        }
        //
        page.setRecords(list);
        page.setCurrent(userKeyWordDTO.getCurrentPage());
        page.setSize(userKeyWordDTO.getPageSize());

        int count = userMapper.getUserTotal(userKeyWordDTO);
        page.setTotal(count);
        return page;
    }

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        userMapper.selectOne(queryWrapper);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }
}
